package com.zhanghp.class027;

import java.io.*;
import java.util.Arrays;

/**
 * 牛客下线了该题目，用左神的代码作为对数器
 *
 * @author zhanghp
 * @since 2024/3/29 13:58
 */
public class Code02_MaxCover {

    public static int max = 10_0001;

    public static int n;

    public static int[][] line = new int[max][2];

    // ==============堆=================

    public static int size;
    public static int[] heap = new int[max];

    public static void main(String[] args) throws IOException {
        // in
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        // out
        PrintWriter out = new PrintWriter(System.out);
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            // 构建数据
            n = (int) in.nval;
            for (int i = 0; i < n; i++) {
                in.nextToken();
                line[i][0] = (int) in.nval;
                in.nextToken();
                line[i][1] = (int) in.nval;
            }
            // 处理
            out.println(compute());
        }
        out.flush();
        out.close();
        br.close();
    }

    public static int compute() {
        size = 0;
        int ans = 0;
        Arrays.sort(line, (o1, o2) -> o1[0] - o2[0]);
        for (int i = 0; i < n; i++) {
            // 1.去除不在重复范围的线
            while (size > 0 && line[i][0] >= heap[0]) {
                pop();
            }
            // 2.添加当前线段的尾
            add(line[i][1]);
            // 3.比较最大的重复线的val，和当前线所重复的值，取最大
            ans = Math.max(ans, size);
        }
        return ans;
    }

    public static void add(int val) {
        heap[size] = val;
        int i = size ++;
        while (heap[i] < heap[(i - 1) / 2]) {
            swap(heap, i, (i - 1) / 2);
            i = (i - 1) / 2;
        }
    }

    public static void pop() {
        int i = 0, l = 1;
        // 首末交换
        swap(heap, i, --size);
        while (l < size) {
            int best = (l + 1) < size && heap[l + 1] < heap[l] ? l + 1 : l;
            if (heap[i] < heap[best]) {
                // 停止循环
                break;
            }
            swap(heap, i, best);
            i = best;
            l = best * 2 + 1;
        }
    }

    public static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}
